﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data.SqlClient;
using jsm.SqlHelper;

namespace dbcaac
{
    public sealed class EntityZhifaDW : EntityBase
    {
        public string zfdwId { get; private set; }
        public string zfdwName { get; private set; }
        public string zfdwAbbr { get; private set; }
        public string zfdwAddr { get; private set; }
        public string zfdwLinkman { get; private set; }
        public string zfdwTel { get; private set; }

        public string pageSize { get; private set; }
        public string pageNumber { get; private set; }

        public override void loadPageData(HttpContext context)
        {
            zfdwName = context.Request["zfdwName"];
            zfdwId = context.Request["id"];
            if (string.IsNullOrEmpty(zfdwId))
            {
                zfdwId = context.Request["zfdwId"];
            }
            zfdwAbbr = context.Request["zfdwAbbr"];
            zfdwAddr = context.Request["zfdwAddr"];
            zfdwLinkman = context.Request["zfdwLinkman"];
            zfdwTel = context.Request["zfdwTel"];

            pageSize = context.Request["rows"];      // 每页的行数
            pageNumber = context.Request["page"];    // 当前页码
        }

        public override string isValid()
        {
            if (string.IsNullOrEmpty(zfdwName))
                return "执法单位名称不能为空";
            if (string.IsNullOrEmpty(zfdwAddr))
                return "执法单位地址不能为空";
            return null;
        }

        public override int getTotal()
        {
            return (int)SqlServerHelper.ExecuteScalar("SELECT count(*) FROM [zhifaDW]");
        }

        protected override int doInsertOperation()
        {
            string sqlText = "INSERT INTO [zhifaDW] (zfdwName, zfdwAbbr, zfdwAddr, zfdwLinkman, zfdwTel)" +
            "VALUES (@zfdwName, @zfdwAbbr, @zfdwAddr, @zfdwLinkman, @zfdwTel)";

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("@zfdwName", zfdwName));
            sqlParams.Add(new SqlParameter("@zfdwAbbr", zfdwAbbr));
            sqlParams.Add(new SqlParameter("@zfdwAddr", zfdwAddr));
            sqlParams.Add(new SqlParameter("@zfdwLinkman", zfdwLinkman));
            sqlParams.Add(new SqlParameter("@zfdwTel", zfdwTel));

            return SqlServerHelper.ExecuteNonQuery(sqlText, sqlParams.ToArray());
        }

        protected override int doDeleteOperation()
        {
            string sqlText = "DELETE FROM [zhifaDW] WHERE (zfdwId = @zfdwId)";

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("@zfdwId", zfdwId));

            return SqlServerHelper.ExecuteNonQuery(sqlText, sqlParams.ToArray());
        }

        protected override int doUpdateOperation()
        {
            string sqlText = "UPDATE [zhifaDW] SET zfdwName = @zfdwName, zfdwAbbr = @zfdwAbbr, zfdwAddr = @zfdwAddr, " +
                "zfdwLinkman = @zfdwLinkman, zfdwTel = @zfdwTel WHERE (zfdwId = @zfdwId)";

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("@zfdwId", zfdwId));
            sqlParams.Add(new SqlParameter("@zfdwName", zfdwName));
            sqlParams.Add(new SqlParameter("@zfdwAbbr", zfdwAbbr));
            sqlParams.Add(new SqlParameter("@zfdwAddr", zfdwAddr));
            sqlParams.Add(new SqlParameter("@zfdwLinkman", zfdwLinkman));
            sqlParams.Add(new SqlParameter("@zfdwTel", zfdwTel));

            return SqlServerHelper.ExecuteNonQuery(sqlText, sqlParams.ToArray());
        }

        public override SqlDataReader executeDataReader()
        {
            string sqlText = "dbcaac_GetPageOfRecords";

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("@pageSize", pageSize));
            sqlParams.Add(new SqlParameter("@currentPage", pageNumber));
            sqlParams.Add(new SqlParameter("@columns", "*"));
            sqlParams.Add(new SqlParameter("@tableName", "zhifaDW"));
            sqlParams.Add(new SqlParameter("@condition", "1=1"));
            sqlParams.Add(new SqlParameter("@ascColumn", "zfdwId"));
            sqlParams.Add(new SqlParameter("@bitOrderType", 0));         // 0 是升序, 1 是降序
            sqlParams.Add(new SqlParameter("@pkColumn", "zfdwId"));
            return SqlServerHelper.ExecuteReaderProc(sqlText, sqlParams.ToArray());
        }
    }
}
